<?php
require 'SiteController.php';
class CartController extends SiteController{
	protected $_billModel;
	protected $_bill_productModel;
	public function init(){
		parent::init();
		$this->_bill_productModel = new Model_BillProduct();
		$this->_billModel = new Model_Bill();
	}
	public function indexAction(){
		$this->view->headTitle("Sieu thi Cuong Phu chuyen: May in HP 1132, May in HP 1102, May in mau HP, May in Canon 2900, May in da nang Canon");
		$this->view->headMeta("description", "Sieu thi Cuong Phu chuyen cung cap cac loai may in chinh hang voi gia ca re nhat tren thi truong, may in hp 1132, May in HP 1102, may in mau hp, May in Canon 2900, May in da nang Canon");
		$this->view->headMeta("keywords", "may in hp 1132, May in HP 1102, may in mau hp, May in Canon 2900, May in da nang Canon, cung cap may in, may in");
		$this->view->headLink()->appendStylesheet(PUBLIC_URL.'/skins/default/css/cart.css');
		$session = new Zend_Session_Namespace();
		$cart = $session->cart;
		$this->view->cart = $cart;
		$totalPrice = 0;
		if(isset($cart)){
			foreach($cart as $product_id => $product){
				$totalPrice += $product["price"] * $product["quantity"];
			}
		}
		$this->view->totalPrice = $totalPrice;
	}
	public function addAction(){
		//kahi báo session
		
		$session = new Zend_Session_Namespace();
		$cart = $session->cart;
		if(!isset($cart)){
			$cart = array();
		}
		//lấy mã sản phẩm
		$product_id = $this->getRequest()->getParam('pid');
		if(isset($product_id) && is_numeric($product_id)){
			
			if(array_key_exists($product_id, $cart)){
				$cart[$product_id]["quantity"] += 1;
			}else {
				$product = $this->_productModel->getProductByID($product_id);
				$cart[$product_id] = array("name" => $product->name, "price" => $product->price, "quantity" => 1,"description" => $product->description, "images" => $product->image);
			}
		}
		$session->cart = $cart;	
	}
	public function updateAction(){
		$this->view->layout()->disableLayout();
		//kahi báo session
		$session = new Zend_Session_Namespace();
		$cart = $session->cart;
		
		
		//lấy mã sản phẩm
		$product_id = $this->getRequest()->getParam('pid');
		$quantity = $this->getRequest()->getParam('quantity');
		if(isset($product_id) && is_numeric($product_id)){
			
			if(array_key_exists($product_id, $cart)){
				$cart[$product_id]["quantity"] = $quantity;
			}
		}
//		$this->view->cart = $cart;
//		$totalPrice = 0;
//		foreach($cart as $product_id => $product){
//			$totalPrice += $product["price"] * $product["quantity"];
//		}
//		$this->view->totalPrice = $totalPrice;
		$session->cart = $cart;	
	}
	public function deleteAction(){
		$this->view->layout()->disableLayout();
		//kahi báo session
		$session = new Zend_Session_Namespace();
		$cart = $session->cart;
		//lấy mã sản phẩm
		$product_id = $this->getRequest()->getParam('pid');
		if(isset($product_id) && is_numeric($product_id)){
			
			if(array_key_exists($product_id, $cart)){
				unset($cart[$product_id]);
			}
		}
//		$this->view->cart = $cart;
//		$totalPrice = 0;
//		foreach($cart as $product_id => $product){
//			$totalPrice += $product["price"] * $product["quantity"];
//		}
//		$this->view->totalPrice = $totalPrice;
		$session->cart = $cart;	
	}
	/**
	 * 
	 * Action để gửi đơn hàng
	 */
	public function sendAction(){
		$this->view->headTitle("Sieu thi Cuong Phu chuyen: May in HP 1132, May in HP 1102, May in mau HP, May in Canon 2900, May in da nang Canon");
		$this->view->headMeta("description", "Sieu thi Cuong Phu chuyen cung cap cac loai may in chinh hang voi gia ca re nhat tren thi truong, may in hp 1132, May in HP 1102, may in mau hp, May in Canon 2900, May in da nang Canon");
		$this->view->headMeta("keywords", "may in hp 1132, May in HP 1102, may in mau hp, May in Canon 2900, May in da nang Canon, cung cap may in, may in");
		$this->view->headLink()->appendStylesheet(PUBLIC_URL.'/skins/default/css/order.css');
		$session = new Zend_Session_Namespace();
		$cart = $session->cart;
		$message =  $session->message;
		$session->__unset($message);
		$request = $this->getRequest();
		if($request->isPost()){
			
			$fullname = $request->getParam('fullname');
			$phone = $request->getParam('phone'); 
			$email=$request->getParam('email');
			$yahoo = $request->getParam('yahoo');
			$address = $request->getParam('address');
			$textare_note = $request->getParam('textare_note');
			
			//lưu dữ liệu và gửi mail cho admin
			try {
			 $bill_id = $this->_billModel->addBill($fullname, $email, $phone, $address);
			//lấy về id bill vừa thêm để thêm vào bill product thông tin cart + bill_id
			
			$product_id_arr = array(); 
			$product_id_arr = array_keys($cart);
			$bill_content = "Đơn đặt hàng mới<br/>Tên khách hàng:".$fullname."<br/>Số điện thoại:".$phone."<br/>Email:"
			.$email."<br/>Yahoo: ".$yahoo."<br/>Địa chỉ: ".$address."<br/>Các sản phẩm đặt mua: <br/>
			<table border='1'cellspaceding='1' cellpadding='1'>
			<th>
				<td>Tên sản phẩm</td>
				<td>Đơn giá</td>
				<td>Số lượng</td>
			</th>";
			//thêm vào bill_product và nội dung mail gửi.
			for($i=0;$i<count($product_id_arr);$i++)
				{
					$this->_bill_productModel->addBillProduct($bill_id,$product_id_arr[$i], $cart[$product_id_arr[$i]]['quantity']);
					$product = $this->_productModel->getProductByID($product_id_arr[$i]);
					$bill_content.=
						"<tr>
							<td>".$product->name."</td>".
							"<td>".$product->price."</td>".
							"<td>".$cart[$product_id_arr[$i]]['quantity']."</td>".
						"</tr>";
				}
			$totalPrice = 0;
			foreach($cart as $product_id => $product){
				$totalPrice += $product["price"] * $product["quantity"];
			}
			$bill_content.="<tr><td colspan='2'>Tổng tiền</td><td>".$totalPrice."</td>";	
				
			}catch (Exception $e){echo $e->getMessage();}
			try {
				 $tr = new Zend_Mail_Transport_Smtp ( 'smtp.gmail.com', array ('auth' => 'login', 'username' => 'chinhpv.hp@gmail.com', 'password' => 'giadinhtoi', 'ssl' => 'ssl', 'port' => 465 ) );
		         Zend_Mail::setDefaultTransport ( $tr );
		         $mail = new Zend_Mail ( 'UTF-8' );
		         $mail->setBodyHtml ($bill_content);
		         $mail->addTo ('chinhpv.hp@gmail.com');
		         $mail->setSubject ('Đơn đặt hàng mới!');
		         $mail->setFrom ('tronghm.dw@gmail.com');
		         $mail->send ();
		         $session->message = "Gửi đơn hàng thành công!";
				$this->view->message = $session->message;
			}catch (Exception $e){
				echo $e->getMessage();
			}
		}
	
	}
	public function printAction(){
		$this->view->layout()->disableLayout();
		$slug = $this->getRequest()->getParam("p");
		if(!isset($slug)){
			$session = new Zend_Session_Namespace();
			$cart = $session->cart;
			$this->view->cart = $cart;
			$totalPrice = 0;
			if(isset($cart)){
				foreach($cart as $product_id => $product){
					$totalPrice += $product["price"] * $product["quantity"];
				}
			}
			$this->view->totalPrice = $totalPrice;
		}else{
			$product = $this->_productModel->getProductBySlug($slug);
			$this->view->product = $product;
		}
	}
}
?>